﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using BI = System.Numerics.BigInteger;
using System.IO.IsolatedStorage;
using System.Collections.Generic;



namespace PodpisyElektorniczne.Classes
{
    public class SchemaProperties
    {
        protected static string Filepath;

        public BI OrderP{ get; set; }
        public BI SubOrderQ { get; set; }
        public BI GeneratorG{ get; set; }
        public BI DashedGeneratorG { get; set; }
        public BI SecretR { get; set; }
        public BI SecretRj { get; set; }

        public SchemaProperties()
        {
            OrderP = BI.Parse("-1");
            SubOrderQ = BI.Parse("-1");
            GeneratorG = BI.Parse("-1");
            DashedGeneratorG = BI.Parse("-1");
            SecretR = BI.Parse("-1");
            SecretRj = BI.Parse("-1");

            if(Filepath == null)
                Filepath = string.Format("{0}{1}{2}","SchemeSettings_",Program.Log.Usser.Login ,".xml");
        }

        public SchemaProperties(byte[][] param)
        {
            if (param.Length == 0) return;
                
            OrderP = new BI(param[0]);
            SubOrderQ = new BI(param[1]);
            GeneratorG = new BI(param[2]);
            DashedGeneratorG = new BI(param[3]);
            SecretR = new BI(param[4]);
            SecretRj = new BI(param[5]);
        }

        public SchemaProperties(List<string> param)
        {
            if (param.Count == 0) return;
            
            GeneratorG = BI.Parse(param[0]);
            DashedGeneratorG = BI.Parse(param[1]);
            OrderP = BI.Parse(param[2]);
            SubOrderQ = BI.Parse(param[3]);            
            SecretR = BI.Parse(param[4]);
            SecretRj = BI.Parse(param[5]);
        }

        public override string ToString()
        {
            return string.Format("{0}{1}{2}{3}{4}{5}", OrderP, SubOrderQ, GeneratorG, DashedGeneratorG, SecretR,
                                 SecretRj);
        }

        public byte[][] ToByteArray()
        {
            return new[] {OrderP.ToByteArray(), SubOrderQ.ToByteArray() , GeneratorG.ToByteArray(), DashedGeneratorG.ToByteArray(), SecretR.ToByteArray(), SecretRj.ToByteArray()};
        }


        public List<string> ToList()
        {
            return new List<string>{
                    this.GeneratorG.ToString(), DashedGeneratorG.ToString(), OrderP.ToString(), SubOrderQ.ToString(), SecretR.ToString(), SecretRj.ToString()
            };        
        }


        public bool XmlSerializeSchemaProperties()
        {
            try
            {
                using(var storage = IsolatedStorageFile.GetMachineStoreForAssembly())
                using(var store = storage.CreateFile(Filepath))
                using(var writer = XmlWriter.Create(store))
                {
                    new XmlSerializer(typeof(List<string>)).Serialize(writer,ToList());
                }
                return true;    
            }
            catch(Exception ex)
            {
                throw ex;
            }
            
        }


        public SchemaSetts XmlDeserializeSchemaProperties()
        {
            try
            {
                using (var storage = IsolatedStorageFile.GetMachineStoreForAssembly())
                {
                    if(storage.FileExists(Filepath) && Filepath != null){ 
                        using (var stream = storage.OpenFile(Filepath, FileMode.Open))
                        using(var rdr = XmlReader.Create(stream))
                        {

                            return new SchemaSetts(new SchemaProperties(
                                (List<string>)(new XmlSerializer(typeof(List<string>)).Deserialize(rdr))), Filepath);
                        }
                    }
                }
            }
            catch (Exception)
            {
                throw;
            }
            return default(SchemaSetts);
        }
    }
}
